package com.xindaoapp.happypet.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.xindaoapp.happypet.R;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass. Activities that contain this fragment
 * must implement the {@link XDBaseFragment.OnFragmentInteractionListener} interface to handle
 * interaction events. Use the {@link XDBaseFragment#newInstance} factory method to create an
 * instance of this fragment.
 */
public class XDBaseFragment extends Fragment implements OnCancelListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    protected View mView;
    protected Context context;
    protected LayoutInflater mInflater;
    protected SharedPreferences mPreferences;
    protected int statusBarHeight;
    
    /** 播放加载时动画 */
    protected FrameLayout loading;
    protected ImageView imageView;
    protected AnimationDrawable animation;
    protected View load_fail;

    /** 初始化数据 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        mInflater = LayoutInflater.from(context);
        // initUi();
    }

    protected View findViewById(int id) {
        return mView.findViewById(id);
    }

    /** 返回布局文件 */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mView;
    }

    @Override
    public void onCancel(DialogInterface arg0) {}

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 新增了一个判断，判断当前页面是否被用户可见
        // if (getUserVisibleHint()) {
        initLoading();
        initUi();
        // }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    private void initLoading() {
        loading = (FrameLayout) getView().findViewById(R.id.loading);
        load_fail = getView().findViewById(R.id.ll_load_fail);
        if (load_fail != null)
            load_fail.setVisibility(View.GONE);
        imageView = (ImageView) getView().findViewById(R.id.imageView);
        if (imageView != null) {
            imageView.setBackgroundResource(R.anim.alertdialog_animation);
            animation = (AnimationDrawable) imageView.getBackground();
            animation.setOneShot(false);
            animation.start();
        }
        loading.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

    }
    protected final void onDataArrived(boolean isArrived) {
        if (isArrived) {
            if (animation != null && null != imageView) {
                animation.stop();
                if (load_fail.getVisibility() == View.VISIBLE) {
                    load_fail.setVisibility(View.GONE);
                }
                loading.setVisibility(View.GONE);
            }
        } else {
            if (animation != null && null != imageView) {
                animation.stop();
                if (loading.getVisibility() == View.GONE) {
                    loading.setVisibility(View.VISIBLE);
                }
                if (load_fail.getVisibility() == View.GONE) {
                    load_fail.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    protected void fragmentResume() {

    }

    protected void initUi() {


    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public boolean getUserVisibleHint() {
        return super.getUserVisibleHint();
    }


}
